package com.srwl.mytx.domain;


import android.os.Parcel;
import android.os.Parcelable;

import com.alibaba.fastjson.annotation.JSONField;
import com.srwl.mytx.greendao.ContactDao;
import com.srwl.mytx.greendao.DaoSession;
import com.srwl.mytx.greendao.InfiniteConnectDao;
import com.srwl.mytx.greendao.UserDao;

import org.greenrobot.greendao.DaoException;
import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Generated;
import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.annotation.Index;
import org.greenrobot.greendao.annotation.ToMany;
import org.greenrobot.greendao.annotation.ToOne;
import org.greenrobot.greendao.annotation.Transient;

import java.util.List;

/**
 * Created by dahai2070 on 2016/10/8 0008.
 */
@Entity
public class Contact implements Parcelable {
    @Id
    private String cId;//我与对方的Contact 表的记录 id
    @Index(unique = true)
    private String tId;//对方的用户id

    private String mId; //我的用户id
    //一对一
    @JSONField(serialize = false)
    @ToOne(joinProperty = "tId")
    private User targetUser;

    private String status;
    private Integer type;
    private String inviteMessage;
    private String groupName;
    //对方给我的查看轨迹的权限，1有权限，0无权限
    private String trace;
    //对方给我的查看定位的权限，1有权限，0无权限
    private String location;
    //对方给我的亲戚互联权限，1有权限，0无权限
    private String relationInfinite;
    //我给对方的查看轨迹权限，1有权限，0无权限
    private String tTrace;
    //我给对方的查看定位权限，1有权限，0无权限
    private String tLocation;
    //我给对方的亲戚互联权限，1有权限，0无权限
    private String tRelationInfinite;
    //无线互联备注名
    private String infiniteTag;
    //无线互联暗号
    private String infiniteKey;
    //无线互联中无限制交流权限，1有权限，0无权限
    private String infiniteChat;
    //无线互联中 向上越级交流权限，1有权限，0无权限
    private String upChat;
    //无线互联中 向下调整组织结构权限（在界面上拖拽以改变contact的从属关系），1有权限，0无权限
    private String adjustStructure;
    //    @ToMany(referencedJoinProperty = "cId")
//    private transient List<InfiniteConnect> infinites;

    private  int position; //分组排序字段 ，服务端不维护，用户换设备登录的话，之前设置的排序失效，

    @JSONField(serialize = false)
    @ToMany(referencedJoinProperty = "cId")
    private List<InfiniteConnect> infiniteConnects;
    //互联无限
    @JSONField(serialize = false)
    @Transient
    private List<Contact> childContactList;
    @JSONField(serialize = false)
    @Transient
    private boolean spreadChildItem = false;
    @JSONField(serialize = false)
    @Transient
    private boolean haveMoreChildContact = true;
    @Transient
    private Contact parentContact;

    public Contact() {
    }

    public String gettId() {
        return tId;
    }

    public void settId(String tId) {
        this.tId = tId;
    }


    public String getcId() {
        return cId;
    }

    public void setcId(String cId) {
        this.cId = cId;
    }



    public String getmId() {
        return mId;
    }

    public void setmId(String mId) {
        this.mId = mId;
    }



    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public Integer getType() {
        return type;
    }

    public void setType(Integer type) {
        this.type = type;
    }

    public String getInviteMessage() {
        return inviteMessage;
    }

    public void setInviteMessage(String inviteMessage) {
        this.inviteMessage = inviteMessage;
    }

    public String getGroupName() {
        return groupName;
    }

    public void setGroupName(String groupName) {
        this.groupName = groupName;
    }


    /**
     * 获取内存中的user,而不是去数据库读取关联的用户信息
     * @return
     */
    @JSONField(name = "targetUser")
    public User getUser() {
        return this.targetUser;
    }

    public List<Contact> getChildContactList() {
        return childContactList;
    }

    public void setChildContactList(List<Contact> childContactList) {
        this.childContactList = childContactList;
    }



    public List<InfiniteConnect> getInfinite() {
        return infiniteConnects;
    }


    public boolean isHaveMoreChildContact() {
        return haveMoreChildContact;
    }
    public void setHaveMoreChildContact(boolean haveMoreChildContact) {
        this.haveMoreChildContact = haveMoreChildContact;
    }

    public Contact getParentContact() {
        return parentContact;
    }

    public void setParentContact(Contact parentContact) {
        this.parentContact = parentContact;
    }



    public String getCId() {
        return this.cId;
    }

    public void setCId(String cId) {
        this.cId = cId;
    }

    public String getTId() {
        return this.tId;
    }

    public void setTId(String tId) {
        this.tId = tId;
    }




    /** To-one relationship, resolved on first access. */
    @Generated(hash = 800583034)
    public User getTargetUser() {
        String __key = this.tId;
        if (targetUser__resolvedKey == null || targetUser__resolvedKey != __key) {
            final DaoSession daoSession = this.daoSession;
            if (daoSession == null) {
                throw new DaoException("Entity is detached from DAO context");
            }
            UserDao targetDao = daoSession.getUserDao();
            User targetUserNew = targetDao.load(__key);
            synchronized (this) {
                targetUser = targetUserNew;
                targetUser__resolvedKey = __key;
            }
        }
        return targetUser;
    }

    /** called by internal mechanisms, do not call yourself. */
    @Generated(hash = 953706043)
    public void setTargetUser(User targetUser) {
        synchronized (this) {
            this.targetUser = targetUser;
            tId = targetUser == null ? null : targetUser.getUId();
            targetUser__resolvedKey = tId;
        }
    }

    /**
     * To-many relationship, resolved on first access (and after reset).
     * Changes to to-many relations are not persisted, make changes to the target entity.
     */
    @Generated(hash = 833506998)
    public List<InfiniteConnect> getInfiniteConnects() {
        if (infiniteConnects == null) {
            final DaoSession daoSession = this.daoSession;
            if (daoSession == null) {
                throw new DaoException("Entity is detached from DAO context");
            }
            InfiniteConnectDao targetDao = daoSession.getInfiniteConnectDao();
            List<InfiniteConnect> infiniteConnectsNew = targetDao
                    ._queryContact_InfiniteConnects(cId);
            synchronized (this) {
                if (infiniteConnects == null) {
                    infiniteConnects = infiniteConnectsNew;
                }
            }
        }
        return infiniteConnects;
    }

    /**
     * Resets a to-many relationship, making the next get call to query for a fresh result.
     */
    @Generated(hash = 1089537322)
    public synchronized void resetInfiniteConnects() {
        infiniteConnects = null;
    }

    /**
     * Convenient call for {@link org.greenrobot.greendao.AbstractDao#delete(Object)}.
     * Entity must attached to an entity context.
     */
    @Generated(hash = 128553479)
    public void delete() {
        if (myDao == null) {
            throw new DaoException("Entity is detached from DAO context");
        }
        myDao.delete(this);
    }

    /**
     * Convenient call for {@link org.greenrobot.greendao.AbstractDao#refresh(Object)}.
     * Entity must attached to an entity context.
     */
    @Generated(hash = 1942392019)
    public void refresh() {
        if (myDao == null) {
            throw new DaoException("Entity is detached from DAO context");
        }
        myDao.refresh(this);
    }

    /**
     * Convenient call for {@link org.greenrobot.greendao.AbstractDao#update(Object)}.
     * Entity must attached to an entity context.
     */
    @Generated(hash = 713229351)
    public void update() {
        if (myDao == null) {
            throw new DaoException("Entity is detached from DAO context");
        }
        myDao.update(this);
    }

    /**
     * called by internal mechanisms, do not call yourself.
     */
    @Generated(hash = 2088270543)
    public void __setDaoSession(DaoSession daoSession) {
        this.daoSession = daoSession;
        myDao = daoSession != null ? daoSession.getContactDao() : null;
    }




    /**
     * Used to resolve relations
     */
    @Generated(hash = 2040040024)
    private transient DaoSession daoSession;
    /**
     * Used for active entity operations.
     */
    @Generated(hash = 2046468181)
    private transient ContactDao myDao;
    @Generated(hash = 551881793)
    private transient String targetUser__resolvedKey;


    public String getTrace() {
        return this.trace;
    }

    public void setTrace(String trace) {
        this.trace = trace;
    }

    public String getLocation() {
        return this.location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public String getTTrace() {
        return this.tTrace;
    }

    public void setTTrace(String tTrace) {
        this.tTrace = tTrace;
    }

    public String getTLocation() {
        return this.tLocation;
    }

    public void setTLocation(String tLocation) {
        this.tLocation = tLocation;
    }

    public String getRelationInfinite() {
        return this.relationInfinite;
    }

    public void setRelationInfinite(String relationInfinite) {
        this.relationInfinite = relationInfinite;
    }

    public String getInfiniteTag() {
        return this.infiniteTag;
    }

    public void setInfiniteTag(String infiniteTag) {
        this.infiniteTag = infiniteTag;
    }

    public String getInfiniteKey() {
        return this.infiniteKey;
    }

    public void setInfiniteKey(String infiniteKey) {
        this.infiniteKey = infiniteKey;
    }

    public String getInfiniteChat() {
        return this.infiniteChat;
    }

    public void setInfiniteChat(String infiniteChat) {
        this.infiniteChat = infiniteChat;
    }

    public String getUpChat() {
        return this.upChat;
    }

    public void setUpChat(String upChat) {
        this.upChat = upChat;
    }

    public String getAdjustStructure() {
        return this.adjustStructure;
    }

    public void setAdjustStructure(String adjustStructure) {
        this.adjustStructure = adjustStructure;
    }

    public String getTRelationInfinite() {
        return this.tRelationInfinite;
    }

    public void setTRelationInfinite(String tRelationInfinite) {
        this.tRelationInfinite = tRelationInfinite;
    }

    public int getPosition() {
        return this.position;
    }

    public void setPosition(int position) {
        this.position = position;
    }

    @Generated(hash = 1204904780)
    public Contact(String cId, String tId, String mId, String status, Integer type,
            String inviteMessage, String groupName, String trace, String location,
            String relationInfinite, String tTrace, String tLocation,
            String tRelationInfinite, String infiniteTag, String infiniteKey,
            String infiniteChat, String upChat, String adjustStructure, int position) {
        this.cId = cId;
        this.tId = tId;
        this.mId = mId;
        this.status = status;
        this.type = type;
        this.inviteMessage = inviteMessage;
        this.groupName = groupName;
        this.trace = trace;
        this.location = location;
        this.relationInfinite = relationInfinite;
        this.tTrace = tTrace;
        this.tLocation = tLocation;
        this.tRelationInfinite = tRelationInfinite;
        this.infiniteTag = infiniteTag;
        this.infiniteKey = infiniteKey;
        this.infiniteChat = infiniteChat;
        this.upChat = upChat;
        this.adjustStructure = adjustStructure;
        this.position = position;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(this.cId);
        dest.writeString(this.tId);
        dest.writeString(this.mId);
        dest.writeParcelable(this.targetUser, flags);
        dest.writeString(this.status);
        dest.writeString(this.inviteMessage);
        dest.writeString(this.groupName);
        dest.writeString(this.trace);
        dest.writeString(this.location);
        dest.writeString(this.relationInfinite);
        dest.writeString(this.tTrace);
        dest.writeString(this.tLocation);
        dest.writeString(this.tRelationInfinite);
        dest.writeString(this.infiniteTag);
        dest.writeString(this.infiniteKey);
        dest.writeString(this.infiniteChat);
        dest.writeString(this.upChat);
        dest.writeString(this.adjustStructure);
        dest.writeInt(this.position);
        dest.writeTypedList(this.infiniteConnects);
        dest.writeTypedList(this.childContactList);
        dest.writeByte(this.spreadChildItem ? (byte) 1 : (byte) 0);
        dest.writeByte(this.haveMoreChildContact ? (byte) 1 : (byte) 0);
        dest.writeParcelable(this.parentContact, flags);
    }

    public String getMId() {
        return this.mId;
    }

    public void setMId(String mId) {
        this.mId = mId;
    }

    protected Contact(Parcel in) {
        this.cId = in.readString();
        this.tId = in.readString();
        this.mId = in.readString();
        this.targetUser = in.readParcelable(User.class.getClassLoader());
        this.status = in.readString();
        this.inviteMessage = in.readString();
        this.groupName = in.readString();
        this.trace = in.readString();
        this.location = in.readString();
        this.relationInfinite = in.readString();
        this.tTrace = in.readString();
        this.tLocation = in.readString();
        this.tRelationInfinite = in.readString();
        this.infiniteTag = in.readString();
        this.infiniteKey = in.readString();
        this.infiniteChat = in.readString();
        this.upChat = in.readString();
        this.adjustStructure = in.readString();
        this.position = in.readInt();
        this.infiniteConnects = in.createTypedArrayList(InfiniteConnect.CREATOR);
        this.childContactList = in.createTypedArrayList(Contact.CREATOR);
        this.spreadChildItem = in.readByte() != 0;
        this.haveMoreChildContact = in.readByte() != 0;
        this.parentContact = in.readParcelable(Contact.class.getClassLoader());
    }

    public static final Creator<Contact> CREATOR = new Creator<Contact>() {
        @Override
        public Contact createFromParcel(Parcel source) {
            return new Contact(source);
        }

        @Override
        public Contact[] newArray(int size) {
            return new Contact[size];
        }
    };

    @Override
    public String toString() {
        return "Contact{" +
                "cId='" + cId + '\'' +
                ", tId='" + tId + '\'' +
                ", mId='" + mId + '\'' +
                ", targetUser=" + targetUser +
                ", status='" + status + '\'' +
                ", type=" + type +
                ", inviteMessage='" + inviteMessage + '\'' +
                ", groupName='" + groupName + '\'' +
                ", trace='" + trace + '\'' +
                ", location='" + location + '\'' +
                ", relationInfinite='" + relationInfinite + '\'' +
                ", tTrace='" + tTrace + '\'' +
                ", tLocation='" + tLocation + '\'' +
                ", tRelationInfinite='" + tRelationInfinite + '\'' +
                ", infiniteTag='" + infiniteTag + '\'' +
                ", infiniteKey='" + infiniteKey + '\'' +
                ", infiniteChat='" + infiniteChat + '\'' +
                ", upChat='" + upChat + '\'' +
                ", adjustStructure='" + adjustStructure + '\'' +
                ", position=" + position +
                ", infiniteConnects=" + infiniteConnects +
                ", childContactList=" + childContactList +
                ", spreadChildItem=" + spreadChildItem +
                ", haveMoreChildContact=" + haveMoreChildContact +
                ", parentContact=" + parentContact +
                ", daoSession=" + daoSession +
                ", myDao=" + myDao +
                ", targetUser__resolvedKey='" + targetUser__resolvedKey + '\'' +
                '}';
    }
}
